package com.zdxlz.fcmp.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdxlz.fcmp.business.entity.DeviceInfoEntity;
import com.zdxlz.fcmp.business.mapper.DeviceInfoMapper;
import com.zdxlz.fcmp.business.service.IDeviceInfoService;
import com.zdxlz.fcmp.business.vo.DeviceInfoVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备信息表 服务实现类
 * </p>
 *
 * @author: aman
 * @since 2025-02-08
 */
@Service
@AllArgsConstructor
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfoEntity> implements IDeviceInfoService {

    /**
     * @description 分页查询设备信息列表
     * @author aman
     * @date 2025/2/11 10:33
     */
    @Override
    public IPage<DeviceInfoVo> getList(Page<DeviceInfoEntity> page, DeviceInfoEntity deviceInfoEntity) {
        LambdaQueryWrapper<DeviceInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(deviceInfoEntity)) {
            queryWrapper
                    .eq(StrUtil.isNotBlank(deviceInfoEntity.getMac()), DeviceInfoEntity::getMac, deviceInfoEntity.getMac())
                    .eq(StrUtil.isNotBlank(deviceInfoEntity.getModel()), DeviceInfoEntity::getModel, deviceInfoEntity.getModel())
                    .like(StrUtil.isNotBlank(deviceInfoEntity.getDeviceAddress()), DeviceInfoEntity::getDeviceAddress, deviceInfoEntity.getDeviceAddress())
                    .like(StrUtil.isNotBlank(deviceInfoEntity.getAccount()), DeviceInfoEntity::getAccount, deviceInfoEntity.getAccount());
        }
        Page<DeviceInfoEntity> deviceInfoPage = baseMapper.selectPage(page, queryWrapper);

        List<DeviceInfoVo> deviceInfoVoList = deviceInfoPage.getRecords().stream().map(this::convertToDeviceInfoVo).collect(Collectors.toList());
        IPage<DeviceInfoVo> deviceInfoVoPage = new Page<>(deviceInfoPage.getCurrent(), deviceInfoPage.getSize(), deviceInfoPage.getTotal());
        deviceInfoVoPage.setRecords(deviceInfoVoList);
        return deviceInfoVoPage;
    }

    @Override
    public List<DeviceInfoEntity> getListByMacs(List<String> macs) {
        // 查询设备信息
        LambdaQueryWrapper<DeviceInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DeviceInfoEntity::getMac, macs);
        return list(queryWrapper);
    }

    /**
     * @Description：设备实体转换为设备响应对象
     * @Author：aman
     * @Date：2025/1/27 12:22
     * @Param：systemTickets
     * @ParamsType：[com.example.fcmp.entity.SystemTickets]
     * @Return：com.example.fcmp.common.resp.SystemTicketsResp
     */
    private DeviceInfoVo convertToDeviceInfoVo(DeviceInfoEntity deviceInfoEntity) {
        DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
        BeanUtil.copyProperties(deviceInfoEntity, deviceInfoVo);
        return deviceInfoVo;
    }
}
